package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-17
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/eduteacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //2、实现根据id查询讲师信息
    //    协议描述           @ApiOperation                        用在controller方法上
    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(//@ApiParam使用在方法上或者参数上，字段说明；表示对参数的添加元数据（说明或是否必填等）
                            @ApiParam(name = "teacher", value = "讲师对象", required = true)
                                @RequestBody EduTeacher teacher){
        boolean update = teacherService.updateById(teacher);
        if(update){
            return R.ok();
        }else{
            return R.error();
        }
    }


    //新增讲师
    //    协议描述           @ApiOperation                        用在controller方法上
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }


    }

    @ApiOperation(value = "根据id查询讲师信息")
    @GetMapping("getTeacherById/{id}")
    public R getTeacherById(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("eduTeacher",eduTeacher);
    }


    //带条件分页查询讲师列表
//    协议描述           @ApiOperation                        用在controller方法上
    @ApiOperation(value = "带条件分页查询讲师列表")
    @PostMapping("getTeacherPageVo/{current}/{limit}")
    public R getTeacherPageVo(@PathVariable Long current,
                              @PathVariable Long limit,
                              @RequestBody TeacherQuery teacherQuery
                              ) {

        //@RequestBody 把json串转成实体类
        //1、取出查询条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();//头衔
        String begin = teacherQuery.getBegin();//查询开始时间
        String end = teacherQuery.getEnd();//查询结束时间

        //2、判断条件是否为空，不为空拼写sql
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        Page<EduTeacher> page = new Page<>(current, limit);
        teacherService.page(page,wrapper);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();


        //1 数据存入map
//        Map<String,Object> map = new HashMap<>();
//        map.put("list",records);
//        map.put("total",total);
//        return R.ok().data(map);
        //2直接拼接
        return R.ok().data("list",records).data("total",total);
    }


    //分页
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
            //@ApiParam使用在方法上或者参数上，字段说明；表示对参数的添加元数据（说明或是否必填等）
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        teacherService.page(pageParam, null);
        List<EduTeacher> records = pageParam.getRecords();//列表数据
        //取两个重要的就行
        long total = pageParam.getTotal();
        //1 数据存入map
//        Map<String,Object> map = new HashMap<>();
//        map.put("list",records);
//        map.put("total",total);
//        return R.ok().data(map);
        //2直接拼接
        return R.ok().data("total", total).data("rows", records);


    }


    //查询所有教师
    @ApiOperation(value = "所有讲师列表")
    @GetMapping
    public R getAllTeacher() {
        //1、错误程序  测试异常
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("list", list);
    }



//根据ID删除讲师
@ApiOperation(value = "根据ID删除讲师")
@DeleteMapping("{id}")
public R delTeacher(@PathVariable String id) {
    boolean remove = teacherService.removeById(id);
    //判断是否有id
    if (remove) {
        return R.ok();
    } else {
        return R.error();
    }


}


}

